package com.kcs.fxmargin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.kcs.fxmargin.bo.ValidateSettlementBO;
import com.kcs.fxmargin.model.TDeals;
import com.kcs.fxmargin.model.TDealsSettle;

public class DealsDao {

	public List<ValidateSettlementBO> search(Connection conn, PreparedStatement pstm, ResultSet rs, TDeals deals) throws Exception {
		
		List<ValidateSettlementBO> validateSettlementBOList = new ArrayList<ValidateSettlementBO>();
		StringBuffer sql 					= new StringBuffer();
		ValidateSettlementBO	validate 	= null;
				
		sql.append(" SELECT d.cif_no, ");
		sql.append(" 	convert(varchar, d.create_dt, 108) input_time, ");
		sql.append(" 	d.deal_id, ");
		sql.append(" 	d.trans_type deal_type, ");
		sql.append(" 	d.outstd_amt amount, ");
		sql.append(" 	s.settle_amt, ");
		sql.append(" 	s.settle_remain, ");
		sql.append(" 	s.settle_date, ");
		sql.append(" 	s.id deals_settle_id, ");
		sql.append(" 	s.row_version ");
		sql.append(" FROM t_deals d ");
		sql.append(" 	LEFT JOIN t_deals_settle s ON d.deal_id = s.deal_id ");
		sql.append(" 	AND s.active_status = 'A' ");
		sql.append(" WHERE d.active_status = 'A' ");
		
		if(deals.getValueDate() != null) sql.append(" 	AND d.value_date = ? ");
		if(deals.getCifNo() != null && !"".equals(deals.getCifNo())) sql.append(" 	AND d.cif_no = ? ");
		
		sql.append(" ORDER BY cif_no, ");
		sql.append(" 	input_time ");
		
		System.out.println("SQL -> " + sql.toString());
		int index = 1;
		
		pstm = conn.prepareStatement(sql.toString());
		
		if(deals.getValueDate() != null) {
			pstm.setDate(index, deals.getValueDate());
			index++;
		}
		
		if(deals.getCifNo() != null && !"".equals(deals.getCifNo())) {
			pstm.setString(index, deals.getCifNo());
			index++;
		}
		
		rs = pstm.executeQuery();
		index = 1;
		
		while(rs.next()){
			
			validate = new ValidateSettlementBO();
			validate.setIndex(index);
			validate.setCifNo(rs.getString("cif_no"));
			validate.setInputTime(rs.getString("input_time"));
			validate.setDealId(rs.getString("deal_id"));
			validate.setDealType(rs.getString("deal_type"));
			validate.setAmount(rs.getBigDecimal("amount"));
			validate.setSettleAmount(rs.getBigDecimal("settle_amt"));
			validate.setSettleRemain(rs.getBigDecimal("settle_remain"));
			validate.setSettleDate((java.util.Date)rs.getDate("settle_date"));
			validate.setDealsSettleId(rs.getString("deals_settle_id"));
			validate.setRowVersion(rs.getInt("row_version"));
			
			validateSettlementBOList.add(validate);
			index++;
		}
		
		System.out.println("validateSettlementBOList size = " + validateSettlementBOList.size());
		
		return validateSettlementBOList;
	}
	
	
	public String update(Connection conn, PreparedStatement pstm,TDealsSettle deals) throws Exception {
		
		StringBuffer sql 		= new StringBuffer();
		
		int update = 0;
		
		sql.append(" update t_deals_settle set ");
		sql.append(" settle_amt = ? , ");
		sql.append(" settle_remain = ? , ");
		sql.append(" row_version = ? , ");
		sql.append(" update_by = ? , ");
		sql.append(" update_dt = getDate() ");
		sql.append(" where id = ? ");
		
		
		System.out.println("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setBigDecimal(1, deals.getSettleAmount());
		pstm.setBigDecimal(2, deals.getSettleRemain());
		pstm.setInt(3, deals.getRowVersion());
		pstm.setString(4, deals.getUpdateBy());
		pstm.setString(5, deals.getId());
		
		update = pstm.executeUpdate();
		
		System.out.println("update = " + update);
		
		return update == 1 ? "s" : "e";
	}
	
	public String insert(Connection conn, PreparedStatement pstm,TDealsSettle deals) throws Exception {
		
		StringBuffer sql 		= new StringBuffer();
		
		int insert = 0;
		
		sql.append(" insert into t_deals_settle (id, deal_id, settle_amt, settle_remain, settle_date, ");
		sql.append(" active_status, create_by, create_dt, update_by, update_dt, row_version) values  ");
		sql.append(" (?, ?, ?, ?, getDate(), ?, ?, getDate(), ?, getDate(), ?) ");
		
		
		System.out.println("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, deals.getId());
		pstm.setString(2, deals.getDealId());
		pstm.setBigDecimal(3, deals.getSettleAmount());
		pstm.setBigDecimal(4, deals.getSettleRemain());
		pstm.setString(5, "A");
		pstm.setString(6, deals.getCreateby());
		pstm.setString(7, deals.getUpdateBy());
		pstm.setInt(8, 1);
		
		insert = pstm.executeUpdate();
		
		System.out.println("insert = " + insert);
		
		return insert == 1 ? "s" : "e";
	}
	
}
